<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline" :form="queryForm">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button type="primary" @click="this.search">查询</a-button>
              <a-button
                type="danger"
                :disabled="selectedRowKeys.length == 0"
                icon="delete"
                @click="del"
                style="margin-left:10px"
                >删除</a-button
              >
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <a-spin :spinning="loading">
      <s-table
        ref="table"
        size="middle"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        bordered
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      >
      </s-table>
    </a-spin>
  </a-card>
</template>

<script>
import { STable } from '@/components'
import { list, del } from '@/api/system/log/log'
export default {
  name: 'Dict',
  components: {
    STable
  },
  data() {
    return {
      // 高级搜索 展开/关闭
      advanced: true,
      // 新增/修改用户弹出层
      visible: false,
      // 禁用表单所有元素
      formDisabled: false,
      // 用户 新增/编辑表单
      formData: {},
      // 数据表单
      form: this.$form.createForm(this),
      // 查询表单
      queryForm: this.$form.createForm(this),
      // 新增/编辑 弹出层加载
      loading: false,
      // 查询参数
      queryParam: {
        pageIndex: 1,
        pageSize: 10
      },
      // 列表字段表头
      columns: [
        {
          title: '名称',
          dataIndex: 'description',
          width: '10%',
          ellipsis: true
        },
        {
          title: '类型',
          dataIndex: 'methodType',
          align: 'center',
          width: '5%'
        },
        {
          title: '路径',
          width: '15%',
          dataIndex: 'methodUrl',
          ellipsis: true
        },
        {
          title: 'IP',
          width: '10%',
          align: 'center',
          dataIndex: 'ip',
          ellipsis: true
        },
        {
          title: '操作人',
          width: '5%',
          align: 'center',
          dataIndex: 'userName',
          ellipsis: true
        },
        {
          title: '响应时间(ms)',
          width: '6%',
          align: 'center',
          dataIndex: 'responseTime',
          ellipsis: true
        },
        {
          title: '请求时间',
          dataIndex: 'createTime',
          width: '10%',
          ellipsis: true
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => this.list(parameter),
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  methods: {
    // 查询数据
    search() {
      this.$refs.table.refresh(true)
    },
    // 列表查询数据
    list(params) {
      let queryParam = {}
      this.queryForm.validateFields((errors, values) => {
        queryParam = {
          ...this.queryParam,
          ...params,
          ...values
        }
      })
      return list(queryParam)
        .then(res => {
          return res.data
        })
        .catch(err => {
          console.log(err)
        })
        .finally(() => {})
    },
    // 选择数据
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    // 确认删除
    del() {
      const _this = this
      this.$confirm({
        centered: true,
        content: h => (
          <div>
            确认删除当前选中的 <b style="color:red;">{this.selectedRowKeys.length}</b> 条数据?
          </div>
        ),
        onOk() {
          _this.deletes(_this.selectedRowKeys.join(','))
        },
        onCancel() {
          _this.search()
        },
        class: 'deleteConfirm'
      })
    },
    // 删除数据
    deletes(ids) {
      del(ids)
        .then(res => {
          if (res.code !== 200) {
            this.$message.error(res.msg)
          } else {
            this.onSelectChange([], [])
          }
        })
        .catch(err => {
          console.log(err)
        })
        .finally(() => {
          this.search()
        })
    }
  }
}
</script>

<style lang="less" scoped>
.search {
  margin-bottom: 54px;
}

.fold {
  width: calc(100% - 216px);
  display: inline-block;
}

.operator {
  margin-bottom: 18px;
}

@media screen and (max-width: 900px) {
  .fold {
    width: 100%;
  }
}
.ant-calendar-picker {
  width: 358px !important;
}
</style>
